﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using saigonbook.Dal;

namespace saigonbook.Biz
{
    public class BIZ_donhang
    {
        public int madonhang;
        public BIZ_user user;
        public string diachigiaohang;
        public int tongtien;
        public DateTime ngaymuahang;
        public DateTime ngaygiaohang;
        public string tinhtrang;

        public int Madonhang
        {
            get { return madonhang; }
            set { madonhang = value; }
        }
        public BIZ_user User
        {
            get { return user; }
            set { user = value; }
        }
        public string Diachigiaohang
        {
            get { return diachigiaohang; }
            set { diachigiaohang = value; }
        }
        public int Tongtien
        {
            get { return tongtien; }
            set { tongtien = value; }
        }
        public DateTime Ngaymuahang
        {
            get { return ngaymuahang; }
            set { ngaymuahang = value; }
        }
        public DateTime Ngaygiaohang
        {
            get { return ngaygiaohang; }
            set { ngaygiaohang = value; }
        }
        public string Tinhtrang
        {
            get { return tinhtrang; }
            set { tinhtrang = value; }
        }

        public List<BIZ_chitietdonhang> Chitietdonhang
        {
            get { return DAL_donhang.Select_chitietdonhang(madonhang); }
        }
        public BIZ_donhang()
        { }
        public BIZ_donhang(int madonhang, BIZ_user user, string diachigiaohang, int tongtien, DateTime ngaymuahang, DateTime ngaygiaohang, string tinhtrang)
        {
            this.madonhang = madonhang;
            this.user = user;
            this.tongtien = tongtien;
            this.ngaymuahang = ngaymuahang;
            this.ngaygiaohang = ngaygiaohang;
            this.diachigiaohang = diachigiaohang;
            this.tinhtrang = tinhtrang;
        }

        public static List<BIZ_donhang> Select_donhang()
        {
            return DAL_donhang.Select_donhang();
        }

        public List<BIZ_chitietdonhang> Select_chitietdonhang()
        {
            return DAL_donhang.Select_chitietdonhang();
        }

        public int soluongbantrongngay(int masach, DateTime ngay)
        {
            int soluong = 0;
            List<BIZ_chitietdonhang> ds = new List<BIZ_chitietdonhang>();
            ds = DAL_donhang.Select_chitietdonhang();
            ds = (from ctdh in ds 
                  where ctdh.sach.masach == masach && ctdh.donhang.ngaymuahang.Date == ngay.Date 
                  select ctdh).ToList();
            foreach (BIZ_chitietdonhang ct in ds)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }

        public void Them_chitietdonhang(BIZ_chitietdonhang dto)
        {
            DAL_donhang.Them_chitietdonhang(dto);
        }

        public void Them_donhang(BIZ_donhang dto, List<BIZ_chitietdonhang> dsctdh)
        {
            DAL_donhang.Them_donhang(dto);
            foreach (BIZ_chitietdonhang ctdh in dsctdh)
            {
                Them_chitietdonhang(ctdh);
            }
        }

        public void Capnhat_donhangtrangthai(BIZ_donhang donhang)
        {
            DAL_donhang.Capnhat_donhang(donhang);
            List<BIZ_chitietdonhang> dssp = new List<BIZ_chitietdonhang>();
            dssp = DAL_donhang.Select_chitietdonhang(donhang.madonhang);
            foreach (BIZ_chitietdonhang ctdh in dssp)
            {
                BIZ_tonkho tk = new BIZ_tonkho();
                tk.ngay = donhang.ngaygiaohang.Date;
                tk.sach = new BIZ_sach();
                tk.sach.masach = ctdh.sach.masach;
                tk.soluong = tk.soluongtonkho(tk);
                tk.soluong = tk.soluong - ctdh.soluong;
                if (tk.kiemtratontaingay(tk))
                    DAL_tonkho.Capnhat_tonkho(tk);
                else
                    DAL_tonkho.Them_tonkho(tk);
            }
        }

        public void Capnhat_donhangkhongtrangthai(BIZ_donhang donhang)
        {
            DAL_donhang.Capnhat_donhang(donhang);
        }

        public void Xoa_donhangchualienlac(BIZ_donhang donhang)
        {
            DAL_donhang.Xoa_chitietdonhang(donhang.madonhang);
            DAL_donhang.Xoa_donhang(donhang);
        }

        public void Xoa_donhangdalienlac(BIZ_donhang donhang)
        {
            List<BIZ_chitietdonhang> dsctdh = new List<BIZ_chitietdonhang>();
            dsctdh = DAL_donhang.Select_chitietdonhang(donhang.madonhang);
            foreach (BIZ_chitietdonhang ctdh in dsctdh)
            {
                BIZ_tonkho tk = new BIZ_tonkho();
                tk.ngay = DateTime.Now.Date;
                tk.sach = new BIZ_sach();
                tk.sach.masach = ctdh.sach.masach;
                tk.soluong = tk.soluongtonkho(tk);
                tk.soluong = tk.soluong + ctdh.soluong;
                if (tk.kiemtratontaingay(tk))
                    DAL_tonkho.Capnhat_tonkho(tk);
                else
                    DAL_tonkho.Them_tonkho(tk);
            }
            DAL_donhang.Xoa_chitietdonhang(donhang.madonhang);
            DAL_donhang.Xoa_donhang(donhang);
        }

        public int laymadonhang()
        {
            int ma = 0;
            List<BIZ_donhang> ds = new List<BIZ_donhang>();
            ds = DAL_donhang.Select_donhang();
            ds = (from dh in ds orderby dh.madonhang descending select dh).Take(1).ToList();
            foreach (BIZ_donhang _dh in ds)
            {
                ma = _dh.madonhang + 1;
                break;
            }
            return ma;
        }

        bool kiemtranamnhuan(int nam)
        {
            if (nam % 4 == 0 && nam % 100 != 0)
                return true;
            return false;
        }

        int layngay(int thang, int nam)
        {
            if (thang == 2)
            {
                if (kiemtranamnhuan(nam))
                    return 29;
                else
                    return 28;
            }
            else
            {
                if (thang == 4 || thang == 6 || thang == 9 || thang == 12)
                    return 30;
                else
                    return 31;
            }
        }

        public void Thongketheothang_donhang(int thang, int nam, int[] soluongsanpham, int[] tongtien)
        {
            List<BIZ_chitietdonhang> dsct = new List<BIZ_chitietdonhang>();
            dsct = DAL_donhang.Select_chitietdonhang();
            dsct = (from c in dsct
                    where c.donhang.ngaygiaohang.Month == thang && c.donhang.ngaygiaohang.Year == nam && c.donhang.tinhtrang.Equals("đã giao")
                    select c).ToList();
            List<BIZ_donhang> ds = new List<BIZ_donhang>();
            ds = Select_donhang();
            ds = (from dh in ds 
                  where dh.ngaygiaohang.Month == thang && dh.ngaygiaohang.Year == nam && dh.tinhtrang.Equals("đã giao") 
                  select dh).ToList();
            for (int i = 1; i < layngay(thang, nam) + 1; i++)
            {
                soluongsanpham[i] = laysoluongsanphamtheongay(i, dsct);
                tongtien[i] = laytongtientheongay(i, ds);
            }
        }

        public int laytongtientheongay(int ngay, List<BIZ_donhang> ds)
        {
            int tong = 0;
            ds = (from d in ds
                  where d.ngaygiaohang.Day == ngay
                  select d).ToList();
            foreach (BIZ_donhang pn in ds)
            {
                tong += pn.tongtien;
            }
            return tong;
        }

        public int laysoluongsanphamtheongay(int ngay, List<BIZ_chitietdonhang> dsct)
        {
            int soluong = 0;
            dsct = (from c in dsct
                    where c.donhang.ngaygiaohang.Day == ngay
                    select c).ToList();
            foreach (BIZ_chitietdonhang ct in dsct)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }

        public void Thongketheonam_donhang(int nam, int[] soluongsanpham, int[] tongtien)
        {
            List<BIZ_donhang> ds = new List<BIZ_donhang>();
            ds = Select_donhang();
            ds = (from dh in ds 
                  where dh.ngaygiaohang.Year == nam && dh.tinhtrang.Equals("đã giao") 
                  select dh).ToList();

            List<BIZ_chitietdonhang> dsct = new List<BIZ_chitietdonhang>();
            dsct = DAL_donhang.Select_chitietdonhang();
            dsct = (from c in dsct
                    where c.donhang.ngaygiaohang.Year == nam && c.donhang.tinhtrang.Equals("đã giao")
                    select c).ToList();
            ds = (from pn in ds 
                  where pn.ngaygiaohang.Year == nam 
                  select pn).ToList();
            for (int i = 1; i < 13; i++)
            {
                soluongsanpham[i] = laysoluongsanphamtheothang(i, dsct);
                tongtien[i] = laytongtientheothang(i, ds);
            }
        }

        public int laytongtientheothang(int thang, List<BIZ_donhang> ds)
        {
            int tong = 0;
            ds = (from d in ds
                  where d.ngaygiaohang.Month == thang
                  select d).ToList();
            foreach (BIZ_donhang pn in ds)
            {
                tong += pn.tongtien;
            }
            return tong;
        }

        public int laysoluongsanphamtheothang(int thang, List<BIZ_chitietdonhang> dsct)
        {
            int soluong = 0;
            dsct = (from c in dsct
                    where c.donhang.ngaygiaohang.Month == thang
                    select c).ToList();
            foreach (BIZ_chitietdonhang ct in dsct)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }
    }
}
